package com.cat.chapter053;

/**
 * @author cat
 * @description https://leetcode.cn/problems/maximum-width-ramp/
 * @create 2025/7/24 10:40
 * @since JDK17
 */

public class Solution01 {
    static int N = ((int) (5e4 + 10));
    static int[] stack = new int[N];
    public int maxWidthRamp(int[] nums) {
        int ans = 0, r = 1, n = nums.length;
        for (int i = 0; i < n; i++) {
            while (r > 0 && nums[stack[r - 1]] > nums[i]) {
                stack[r++] = i;
            }
        }

        for (int i = n - 1; i >= 0 && r > 0; i--) {
            while (r > 0 && nums[stack[r - 1]] <= nums[i]) {
                ans = Math.max(ans, i - stack[--r]);
            }
        }
        return ans;
    }
}
